package com.sqxgg.drugstore.security.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashSet;

/**
 * UserDetail 类
 * Created by Fant.J.
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

//    @Autowired
//    private //在这里注入mapper，再想ia面根据用户名做信息查找



    @Autowired
    private PasswordEncoder passwordEncoder;


    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 加载用户数据 , 返回UserDetail 实例
     * @param username  用户登录username
     * @return  返回User实体类 做用户校验
     * @throws UsernameNotFoundException
     */
    /**
     * 重写PasswordEncoder  接口中的方法，实例化加密策略
     * @return 返回 BCrypt 加密策略
     */
//    @Bean
//    public PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名:"+username);
        String password = passwordEncoder.encode("123456");
        //User三个参数   (用户名+密码+权限)
        //根据查找到的用户信息判断用户是否被冻结
        logger.info("数据库密码:"+password);

        LoginUser loginUser = adapter(username,password);
        loginUser.setUsername(username);
        return loginUser;
//        return new UserDetails("admin","123456",
//                true,true,true,true,
//                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));

    }

    private LoginUser adapter(String name ,String passWord) {
        LoginUser loginUser = new LoginUser();
        Collection<GrantedAuthority> collection = new HashSet<>();
        loginUser.setId("123");
        loginUser.setAuthorities(collection);
        loginUser.setPassword(passWord);
        loginUser.setAccountNonExpired(true);
        loginUser.setAccountNonLocked(true);
        loginUser.setCredentialsNonExpired(true);
        loginUser.setEnabled(true);
        return loginUser;
    }

}
